package com.hycreate.platform.modules.service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.persistence.Page;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.common.web.BaseController;

/**
 * 服务网点Controller
 * @author hoader
 * @version 2017-01-10
 */
@Controller
@RequestMapping(value = "${adminPath}/service/sfService")
public class SfServiceController extends BaseController {

	@Autowired
	private SfServiceService sfServiceService;
	
	@ModelAttribute
	public SfService get(@RequestParam(required=false) String id) {
		SfService entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sfServiceService.get(id);
		}
		if (entity == null){
			entity = new SfService();
		}
		return entity;
	}
	
	@RequiresPermissions("service:sfService:view")
	@RequestMapping(value = {"list", ""})
	public String list(SfService sfService, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SfService> page = sfServiceService.findPage(new Page<SfService>(request, response), sfService); 
		model.addAttribute("page", page);
		return "modules/service/sfServiceList";
	}

	@RequiresPermissions("service:sfService:view")
	@RequestMapping(value = "form")
	public String form(SfService sfService, Model model) {
		model.addAttribute("sfService", sfService);
		return "modules/service/sfServiceForm";
	}

	@RequiresPermissions("service:sfService:edit")
	@RequestMapping(value = "save")
	public String save(SfService sfService, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sfService)){
			return form(sfService, model);
		}
		sfServiceService.save(sfService);
		addMessage(redirectAttributes, "保存服务网点成功");
		return "redirect:"+Global.getAdminPath()+"/service/sfService/?repage";
	}
	
	@RequiresPermissions("service:sfService:edit")
	@RequestMapping(value = "delete")
	public String delete(SfService sfService, RedirectAttributes redirectAttributes) {
		sfServiceService.delete(sfService);
		addMessage(redirectAttributes, "删除服务网点成功");
		return "redirect:"+Global.getAdminPath()+"/service/sfService/?repage";
	}
}